package com.yiqi.zhiyuan.feature.main.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;

import com.luck.picture.lib.permissions.RxPermissions;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.romgim.SealAppContext;
import com.yiqi.zhiyuan.feature.login.view.LoginActivity;
import com.yiqi.zhiyuan.framework.ZYConstants;

import io.reactivex.functions.Consumer;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.login.view
 * @ClassName: UploadCertificatesActivity
 * @Description: 启动页
 * @Author:
 * @CreateDate: 2019/3/12 10:10
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/12 10:10
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SplashActivity extends BaseActivity {

    RxPermissions mRxPermissions;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initInjector() {

    }


    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

    }


    @SuppressLint("CheckResult")
    @Override
    protected void initData() {

        mRxPermissions = new RxPermissions(this);

        mRxPermissions
                .request(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.CALL_PHONE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) {
                       splash();
                    } else {
                        // At least one permission is denied
                    }
                });

    }

    private void loginSuccess(UserInfoBo value) {
        if (value != null) {
            AM.user().saveLoginResultCache(value == null ? null : value);
            RongIM.getInstance().setCurrentUserInfo(new UserInfo(value.getId(), value.getUsername(),
                    TextUtils.isEmpty(value.getAvatarUrl()) ? Uri.parse("") : Uri.parse(value.getAvatarUrl())));
            RongIM.getInstance().setMessageAttachedUserInfo(true);
            RongIM.connect(ZYConstants.RONG_TOKEN, SealAppContext.getInstance().getConnectCallback());
        }

    }

    private void splash() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (AM.user().isLogin()) {
                        loginSuccess(AM.user().getLoginResultCache());
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        finish();
                    } else {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();
                    }
                }
            }
        }).start();
    }

}
